<?php
//require section
require_once dirname(__FILE__) . '/shared/config/config.php';
require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_session.php';
//require_once DOCUMENT_ROOT . DIR_INCLUDE . 'check_outside_booking.php';
require_once DOCUMENT_ROOT . DIR_SHARED_API . 'api_security.php';
$title = 'Login Page';

$email = "";
$password = "";

if (!empty($_POST)) {
    if (!empty($_POST['Email'])) {
        $email = $_POST['Email'];
    }
    if (!empty($_POST['Password'])) {
        $password = $_POST['Password'];
    }

    $error_show = "";
    $result = api_security::login($email, $password);
    if ($result['user_login'] === 'logined') {
        $_SESSION['user_login'] = 'logined';
        $_SESSION['user_info'] = $result;
        if ($result['role_id'] == 1) {
            lib_redirect::Redirect('/admin/index.php');
        } else {
            if (empty($result['cinema_id'])) {
                lib_redirect::Redirect('/index.php');
            } else {
                lib_redirect::Redirect('/admin/user_cinema.php');
            }
        }

        } else {
            $error_show = "Login failed.";
        }
}
?>

<!DOCTYPE html>
<html>
    <head>
        <?php
        require_once DOCUMENT_ROOT . DIR_INCLUDE . 'head.php';
        ?>
    </head>
    <body>
        <div class="ts-body-container">
            <div class="ts-wrapper">
                <div class="padding-top-large row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'header.php';
                    ?>
                    <div class="ui segment breadcrumb" style="margin-top: 25px; margin-left: 20px;">
                        <a class="section" href="index.php">Home</a>
                        <i class="right arrow icon divider"></i>
                        <a class="active section">Login</a>
                    </div>
                </div>
                <div class="ui grid">
                    <div class="row">
                        <div class="column">
                            <h1>Login Now</h1>
                            <form name="form_login" id="form_login" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>">
                                <?php if (!empty($error_show)) { ?>
                                    <div class="ui error message">
                                        <i class="close icon"></i>
                                        <div class="header">
                                            <?php echo $error_show; ?>
                                        </div>
                                    </div>
                                    <?php
                                } else if (!empty($_SESSION['user_login']) && $_SESSION['user_login'] === "logined") {
                                    ?>
                                    <div class="ui segment">
                                        <div class="ui active dimmer">
                                            <div class="ui text loader">Loading</div>
                                        </div>
                                        <p>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec odio. Quisque volutpat mattis eros. Nullam malesuada erat ut turpis. Suspendisse urna nibh, viverra non, semper suscipit, posuere a, pede.</p>
                                    </div>
                                    <script type="text/javascript">
                                        navigate("<?php echo LINK_ROOT . "/index.php"; ?>");
                                    </script>
                                <?php }
                                ?>
                                <div class="ui form segment">
                                    <div class="field">
                                        <label for="Email">Email</label>
                                        <input id="Email" name="Email" value="<?php echo $email; ?>" placeholder="Email" type="text">
                                    </div>

                                    <div class="field">
                                        <label for="Password">Password</label>
                                        <input id="Password" name="Password" type="password">
                                    </div>

                                    <div class="large animated fade ui orange button" onclick="submitLogin();">
                                        <div class="visible content">Submit</div>
                                        <div class="hidden content"><i class="ok sign icon"></i></div>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
                <div class="row">
                    <?php
                    require_once DOCUMENT_ROOT . DIR_INCLUDE . 'footer.php';
                    ?>
                </div>
            </div>
        </div>
    </body>
</html>